<template>
    <el-container>
        <el-container class="is-vertical">
            <div class="panel-search-box">
                <div class="panel-search">
                    <el-input v-model="search.name" placeholder="名称" clearable  class="w165"></el-input>
                    <el-select v-model="search.categoryId" placeholder="类型" clearable filterable class="w165">
						<el-option :label="item.name" :value="item.id" v-for="(item,index) in categoryData" :key="index"></el-option>
					</el-select>
                    <el-select v-model="search.levelId" placeholder="学段" clearable filterable class="w165">
						<el-option :label="item.name" :value="item.id" v-for="(item,index) in levelData" :key="index"></el-option>
					</el-select>
                    <el-select v-model="search.difficultyId" placeholder="难度" clearable filterable class="w165">
						<el-option :label="item.name" :value="item.id" v-for="(item,index) in difficultyData" :key="index"></el-option>
					</el-select>
                    <el-select v-model="search.bookId" placeholder="教材" clearable filterable class="w165">
						<el-option :label="item.name" :value="item.id" v-for="(item,index) in bookData" :key="index"></el-option>
					</el-select>
                    <el-select v-model="search.isTrial" placeholder="体验课" clearable>
                        <el-option label="是" :value="1"></el-option>
                        <el-option label="否" :value="0"></el-option>
                    </el-select>
                    <el-select v-model="search.status" placeholder="状态" clearable>
                        <el-option label="启用" :value="1"></el-option>
                        <el-option label="禁用" :value="0"></el-option>
                    </el-select>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                </div>
            </div>
            <el-main class="nopadding">
                <scTable ref="table" :apiObj="$API.course.list" remoteSort @dataChange="dataChange">
                    <el-table-column label="序号" prop="index" width="70" align="left"></el-table-column>
                    <el-table-column label="名称" prop="name" align="center"></el-table-column>
                    <el-table-column label="类型" prop="category" width="120" align="center">
                        <template #default="scope">
                            {{scope.row.category  ? scope.row.category.name : ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="学段" prop="level" width="120" align="center">
                        <template #default="scope">
                            {{scope.row.level  ? scope.row.level.name : ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="难度" prop="difficulty" width="120" align="center">
                        <template #default="scope">
                            {{scope.row.difficulty  ? scope.row.difficulty.name : ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="教材" prop="book" width="120" align="center">
                        <template #default="scope">
                            {{scope.row.book  ? scope.row.book.name : ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="词汇数量" prop="words" width="120" align="center">
                    </el-table-column>
                    <el-table-column label="体验课" prop="isTrial" width="120" align="center">
                        <template #default="scope">
                            {{scope.row.isTrial.name || ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="状态" prop="status" width="120" align="center">
                        <template #default="scope">
                            <el-tag size="mini" v-if="scope.row.status.value == 0" type="danger">{{scope.row.status.name}}</el-tag>
                            <el-tag size="mini" v-if="scope.row.status.value == 1" type="success">{{scope.row.status.name}}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="创建时间" prop="createdAt" width="150" align="center"></el-table-column>
                    <el-table-column label="操作" fixed="right" align="center" width="220">
                        <template #default="scope">
                            <el-button type="text" size="small" @click="table_show(scope.row, scope.$index)">查看</el-button>
                        </template>
                    </el-table-column>
                </scTable>
            </el-main>
        </el-container>
    </el-container>

    <detail-dialog v-if="dialog.detail" ref="detailDialog" @closed="dialog.detail=false"></detail-dialog>

</template>

<script>
import detailDialog from './detail'
export default {
    components: {
        detailDialog
    },
    data() {
        return {
            dialog: {
                detail: false
            },
            search: {
                name: null,
            },
            categoryData: [],
            levelData: [],
            difficultyData: [],
            bookData: [],
        }
    },
    mounted() {
        this.getAllCategory()
        this.getAllLevel()
        this.getAllDifficulty()
        this.getAllBook()
    },
    methods: {
        dataChange(res, tableData) {
			let total = res.data.total
            let perPage = res.data.perPage
            let page = (this.$refs.table && this.$refs.table.currentPage) || 1
            tableData.forEach((item, index) => {
				item.index = total - (page * perPage + index - perPage)
            })
        },
        getAllCategory() {
            this.$API.course.category.all.get().then(res => {
                this.categoryData = res.data
            })
        },
        getAllLevel() {
            this.$API.course.level.all.get().then(res => {
                this.levelData = res.data
            })
        },
        getAllDifficulty() {
            this.$API.course.difficulty.all.get().then(res => {
                this.difficultyData = res.data
            })
        },
        getAllBook() {
            this.$API.course.book.all.get().then(res => {
                this.bookData = res.data
            })
        },
        //查看
        table_show(row) {
            this.dialog.detail = true
			this.$nextTick(() => {
				this.$refs.detailDialog.open(row.id)
			})
        },
        // 搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        //本地更新数据
        handleSuccess() {
            this.$refs.table.refresh()
        }
    }
}
</script>

<style>
.position {
    display: flex;
    align-items: center;
}
.position-item {
    margin-right: 5px;
}
.position-item:last-child {
    margin-right: 0;
}
</style>
